package jp.co.nec.parser;

import java.util.ArrayList;

import jp.co.nec.tokens.Token;
import jp.co.nec.tokens.Token.TOKENTYPE;
import jp.co.nec.tokens.Token.DELIMTYPE;

public class Validator {
	
	public static int index;
	public static Token prevType;
	
	/* stub function for digitValid function */
	private static boolean digitValid (ArrayList<Token> myList, int index) {
		if (index == myList.size() - 1)
			return true;
		if (myList.get(index + 1).getTokenType() == TOKENTYPE.BLANKSPACE) {
			while (myList.get(index + 1).getTokenType() != TOKENTYPE.OPERATOR && 
					myList.get(index + 1).getTokenType() != TOKENTYPE.DELIMITER) {
				System.out.println(index + " Token Type: " + myList.get(index).getTokenType());
				index++;
				if (index == myList.size() - 1)		// without this, while tests out of range at last index
					break;
				if (myList.get(index).getTokenType() == TOKENTYPE.DIGIT) {
					System.out.println("invalid digit format.");
					return false;
				}
			}
		}
		return true;
	}
	/* stub function for opValid function */
	private static boolean opValid (Token myToken) {
		
		return false;
	}

	/* stub function for delimValid */
	private static boolean delimValid(ArrayList<Token>arrayList, int index, Token prevType) {
		if (index == arrayList.size() - 1 &&
			arrayList.get(index).getDelimType() == DELIMTYPE.CLOSEDELIM) {
			System.out.println("Invalid Delimiter encountered");
			return false;
		}
		return false;
	}
	
	
	
	/* function for isValid */
	public static boolean isValid(ArrayList<Token> arrayList) {
		index = 0;
		prevType = null;
		
		while (index != arrayList.size()) {
			prevType = arrayList.get(index);
			
			switch (prevType.getTokenType()) {
				case INVALID:
					System.out.println("Invalid Token");
					return false;
				case BLANKSPACE:
					System.out.println("Blank Space");
					break;
				case DIGIT:
					System.out.println("DIGIT");
					if (!digitValid(arrayList, index))  { return false; }
					else 
						break;
				case OPERATOR:
					System.out.println("OPERATOR " + prevType.getOpValue());
					break;
				case DELIMITER:
					System.out.println("DELIMITER");
					if (!delimValid(arrayList, index, prevType)) {
						/* Add function here */
					} 
					else { 
						break;
					}
			}
			
			index++;
				
		}
		
		return true;
	}
	
	public static void main (String args[]) {
		
		/* Create test tokens ( 3 + 3 ) */
		Token tok1 = new Token("DELIMITER");
		tok1.setDelimType(DELIMTYPE.OPENDELIM);
		tok1.setDelimValue('(');
		tok1.setTokenType(TOKENTYPE.DELIMITER);
		Token tok2 = new Token("BLANK");
		tok2.setSpace(true);
		tok2.setTokenType(TOKENTYPE.BLANKSPACE);
		Token tok3 = new Token("DIGIT");
		tok3.setNumValue(10);
		tok3.setTokenType(TOKENTYPE.DIGIT);
		Token tok4 = new Token("BLANK");
		tok4.setSpace(true);
		tok4.setTokenType(TOKENTYPE.BLANKSPACE);
		Token tok5 = new Token("OPERATOR");
		tok5.setOpValue('+');
		tok5.setOpPriority(2);
		tok5.setTokenType(TOKENTYPE.OPERATOR);
		Token tok6 = new Token("BLANK");
		tok6.setSpace(true);
		tok6.setTokenType(TOKENTYPE.BLANKSPACE);
		Token tok7 = new Token("DIGIT");
		tok7.setNumValue(20);
		tok7.setTokenType(TOKENTYPE.DIGIT);
		Token tok8 = new Token("BLANK");
		tok8.setSpace(true);
		tok8.setTokenType(TOKENTYPE.BLANKSPACE);
		Token tok9 = new Token("DELIMITER");
		tok9.setDelimType(DELIMTYPE.CLOSEDELIM);
		tok9.setDelimValue(')');
		tok9.setTokenType(TOKENTYPE.DELIMITER);
		
		ArrayList<Token> arrayList = new ArrayList<Token>();
		
		/* Add the tokens to arrayList */
		/* arrayList.add(tok1);
		arrayList.add(tok2);
		arrayList.add(tok3);
		arrayList.add(tok4);
		arrayList.add(tok5);
		arrayList.add(tok6);
		arrayList.add(tok7);
		arrayList.add(tok8);
		arrayList.add(tok9);
		*/
		arrayList.add(tok2);
		arrayList.add(tok2);
		arrayList.add(tok2);
		arrayList.add(tok2);
		arrayList.add(tok2);
		arrayList.add(tok3);
		arrayList.add(tok2);
		arrayList.add(tok2);
		arrayList.add(tok2);
		arrayList.add(tok2);
		arrayList.add(tok2);
		arrayList.add(tok3);
		
		System.out.println("Size of array is " + arrayList.size());
		
		boolean temp = isValid(arrayList);
		
		if (temp == true) {
			System.out.println("isValid is true");
		}
		
	}

}
